﻿niggles.Register({
    Initialise: function (niggles) {
        var me = this;
        me['edit'] = {
            Execute: function (niggles, path) {
                var editType = path[3];
                var $deck = $('#admin-main-menu-deck');

                if ($deck.attr('editType') != editType) {
                    this.SetupEditType($deck, editType);
                }

                if (path.length > 4) {
                    var $body = $('#admin-edit-layout').data['body'];
                    $body.contents().remove();
                    var editItem = niggles._snippets.editItem.Execute();
                    $(editItem.find('.item-type')[0]).html(path[4]);
                    $body.append(editItem);
                }
            },

            SetupEditType: function ($deck, editType) {
                var types = niggles.ServiceAgent.GetData('types');
                var editTypeData = jQuery.grep(types, function (ele, index) { return ele.Value === editType; })[0];
                var detailLink = jQuery.grep(editTypeData.Links, function (ele, index) { return ele.rel === "detail"; })[0]
                var detailLinkUri = detailLink.uri;

                var dataName = 'typeValues-' + editType;
                var editTypeDetailData = niggles.ServiceAgent.GetData(dataName, detailLinkUri);

                $deck.attr('editType', editType);
                $deck.contents().remove();

                $deck.append($('<h1>Editing ' + editTypeData.PluralName + '</h1>'));

                var list = niggles._snippets.list.Execute();
                list.addClass('admin-edit-menu');
                list.SetHeadings([editTypeData.Name]);
                $(editTypeDetailData.Items).each(function () {
                    list.AddRow([this.Name], '/admin/edit/'+editType+'/'+this.Name);
                });

                list.OnSelected.push(function ($row, $list) { niggles._controller.NavTo($row.attr('tag')); });

                var splitLayout = niggles._snippets.splitLayout.Execute();
                splitLayout.SetBarWidth(150);
                splitLayout.attr("id", "admin-edit-layout");
                splitLayout.Bar.append(list);

                $deck.append(splitLayout);
            }
        }
        niggles._controller.admin = this;
    },
    Execute: function (niggles, path) {
        var me = this;
        if (path.length == 2) {
            var types = niggles.ServiceAgent.GetData('types', '/types/1234');

            $('#deck').contents().remove();

            var splitLayout = niggles._snippets.splitLayout.Execute();

            var list = niggles._snippets.list.Execute();
            list.addClass('admin-menu-main');
            list.SetHeadings(['Select a type to edit']);
            $(types).each(function () {
                list.AddRow([this.PluralName], '/admin/edit/' + this.Value);
            });

            list.OnSelected.push(function ($row, $list) { niggles._controller.NavTo($row.attr('tag')); });

            splitLayout.SetBarWidth(200);
            splitLayout.Bar.append(list);
            splitLayout.Body.append($('<div id="admin-main-menu-deck" class="admin-subdeck">Select the type you would like to edit from the menu on the left...</div>'));

            $('#deck').append(splitLayout);

            niggles._snippets.menu.SetWhereAmI(['Admin', 'Operations']);
        } else {
            var action = path[2];
            me[action].Execute(niggles, path);
        }
    }
});